package com.communication.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.communication.json.CommunicationEntity;
import com.communication.json.JsonResult;
import com.communication.service.CommunicationMeter;
import com.communication.service.CommunicationTerminal;
import com.file.service.TerminalService;
import com.protocol.sentjson.SocketData;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/communication")
public class Communication {

	@Value("${socket.port}")
	private String socketport;

	@Autowired
	private TerminalService terminalservice;

	@Autowired
	private CommunicationTerminal communicationTerminal;
	@Autowired
	private CommunicationMeter communicationMeter;

	@ApiOperation(value = "直抄数据发送", notes = "")
	@RequestMapping(value = "/sent", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult<CommunicationEntity> sent(@RequestBody JsonResult<CommunicationEntity> jsonResult,
			HttpServletRequest request) {

		String terminaladdress = terminalservice
				.findTerminaladdressByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));

		String frame = null;
		if (jsonResult.getState() == 3301) {
			// 终端
			frame = communicationTerminal.Enframe(jsonResult);
		}

		if (jsonResult.getState() == 3300) {
			// 电表
			frame = communicationMeter.Enframe(jsonResult);
		}

		// 发送
		if (frame != null && !frame.equals("")) {
			// http请求
			String urlsent = "http://" + socketport + "/SocketControl/sent/realtime";
			RestTemplate restTemplatesent = new RestTemplate();
			// 组发送内容
			JsonResult<SocketData> sentjson = new JsonResult<>();
			SocketData socketData = new SocketData();
			socketData.setKey(request.getSession().getId());
			socketData.setMessage(frame);
			socketData.setTerminaladdress(terminaladdress);
			sentjson.setData(socketData);

			@SuppressWarnings("rawtypes")
			ResponseEntity<JsonResult> returnjson = restTemplatesent.postForEntity(urlsent, sentjson, JsonResult.class);
			// 终端不在线，终端通讯中
			jsonResult.setState(returnjson.getBody().getState());
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) returnjson.getBody().getData();
			request.getSession().setAttribute("randon", map.get("randonint"));
		}

		return jsonResult;
	}

	@ApiOperation(value = "数据接收", notes = "")
	@RequestMapping(value = "/receive", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult<CommunicationEntity> receive(@RequestBody JsonResult<CommunicationEntity> jsonResult,
			HttpServletRequest request) {

		String terminaladdress = terminalservice
				.findTerminaladdressByTerminalid(Integer.valueOf(jsonResult.getData().getTerminalID()));

		// http请求
		String urlsent = "http://" + socketport + "/SocketControl/receive/realtime";
		RestTemplate restTemplatesent = new RestTemplate();
		// 组发送内容
		JsonResult<SocketData> sentjson = new JsonResult<>();
		SocketData socketData = new SocketData();
		socketData.setKey(request.getSession().getId());
		socketData.setTerminaladdress(terminaladdress);
		socketData.setRandonint(request.getSession().getAttribute("randon").toString());
		sentjson.setData(socketData);

		@SuppressWarnings("rawtypes")
		ResponseEntity<JsonResult> returnjson = restTemplatesent.postForEntity(urlsent, sentjson, JsonResult.class);

		if (returnjson.getBody().getState() == 3100) {
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) returnjson.getBody().getData();
			String[] datas = map.get("message").toString().split("-");
			// 解析帧
			if (jsonResult.getState() == 3301) {
				// 终端
				jsonResult = communicationTerminal.DeFrame(jsonResult, datas[0], datas[1]);
			}

			if (jsonResult.getState() == 3300) {
				// 电表
				jsonResult = communicationMeter.DeFrame(jsonResult, datas[0], datas[1]);
			}
			jsonResult.setState(returnjson.getBody().getState());
		}

		jsonResult.setState(returnjson.getBody().getState());
		return jsonResult;
	}

}
